/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.magostini.mailservice.sender;

import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author marco
 * 
 */

public class AcceptFulFillSender implements Runnable{
     Logger log =Logger.getLogger("INTEGRATION-MAILSERVICE");
   


    private JavaMailSender mailSender;
    private String ffText;
    private String idWish;
    private String ffLink;
    private String ffUrlImage;
    private String ffCreatorUsername;
    private String ffCreatoremail;
    private String userAuthUsername;
    private String userAuthEmail;
    private String wishTitle;
    private String wishText;
    private String wishUrlImage;
    private String wishLink;

    private VelocityEngine velocityEngine;

    public AcceptFulFillSender(JavaMailSender mailSender,
            String ffText,
            String ffLink,
            String ffUrlImage,
            String ffCreatorUsername,
            String ffCreatoremail,
            String userAuthUsername,
            String userAuthEmail,
            String idWish,
            String wishTitle,
            String wishText,
            String wishUrlImage,
            String wishLink,
            VelocityEngine velocityEngine) {
        this.mailSender = mailSender;
        this.ffText = ffText;
        this.ffLink = ffLink;
        this.ffUrlImage = ffUrlImage;
        this.ffCreatorUsername = ffCreatorUsername;
        this.ffCreatoremail = ffCreatoremail;
        this.userAuthUsername = userAuthUsername;
        this.userAuthEmail = userAuthEmail;
        this.idWish = idWish;
        this.wishTitle = wishTitle;
        this.wishText = wishText;
        this.wishUrlImage = wishUrlImage;
        this.wishLink = wishLink;
        this.velocityEngine = velocityEngine;
    }


   
    

    @Override
    public void run() {

        
        MimeMessagePreparator preparatorWishOwner = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {
                        
                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(userAuthEmail);
                        message.setSubject("WaveWish - "+userAuthUsername+ " hai accettato la proposta per il tuo desiderio !");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...
                        
                        Map model = new HashMap();
                        
                        model.put("username", userAuthUsername);
                        model.put("usernameFulFIll", ffCreatorUsername);
                        model.put("fftesto", ffText);
                        model.put("fflink", ffLink);
                        model.put("isfflinkPresent", ffLink!=null);
                        model.put("ffUrlImage", ffUrlImage);
                        model.put("idWish", idWish);
                        model.put("wishTitle", wishTitle);
                        model.put("wishText", wishText);
                        model.put("wishUrlImage", wishUrlImage);
                        model.put("wishLink", wishLink);
                        model.put("iswishLinkPresent", wishLink!=null);
                        

                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/acceptFulFill/wishOwner.vm", model);
                        log.debug("set Text ");
                        message.setText(text, true);

                        log.debug("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };


          MimeMessagePreparator preparatorFulFill = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {

                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(ffCreatoremail);
                        message.setSubject("WaveWish - "+userAuthUsername+" ha accettato la tua proposta per esaudire il suo esiderio");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...

                        Map model = new HashMap();

                       model.put("username", ffCreatorUsername);
                        model.put("usernameWishOwner", userAuthUsername);
                        model.put("fftesto", ffText);
                        model.put("fflink", ffLink);
                        model.put("isfflinkPresent", ffLink!=null);
                        model.put("ffUrlImage", ffUrlImage);
                        model.put("idWish", idWish);
                        model.put("wishTitle", wishTitle);
                        model.put("wishText", wishText);
                        model.put("wishUrlImage", wishUrlImage);
                        model.put("wishLink", wishLink);
                        model.put("iswishLinkPresent", wishLink!=null);

                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/acceptFulFill/fulfillOwner.vm", model);
                        log.debug("set Text ");
                        message.setText(text, true);

                        log.debug("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };
                log.info("sendig emails ");

                try{
                        mailSender.send(preparatorWishOwner);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR ACCEPT FULFILL TO "+userAuthEmail,e);

                    return;
                }
                log.info("EMAIL SEND FOR ACCEPT FULFILL TO "+userAuthEmail);



                 try{
                        mailSender.send(preparatorFulFill);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR ACCEPT FULFILL TO "+ffCreatoremail ,e);

                    return;
                }
                log.info("EMAIL SEND FOR ACCEPT FULFILL TO "+ffCreatoremail);
    }

}
